package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> getAllNursingPlan() {
        List<NursingLevelVo> voList = nursingLevelMapper.selectAllNursingLevels();
        return voList;
    }

    /**
     * 新增护理等级
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(NursingLevelDto dto) {
        //判断护理等级名称是否重复
        Integer count = nursingLevelMapper.selectCountNurLevelName(dto.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPLEVEL_NAME_EXICT);
        }
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.insertNursingLevel(nursingLevel);
    }

    /**
     * 条件分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> pageQuery(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.selectNursingLevelsByPage(name, status);

        return PageResponse.of(page, NursingLevelVo.class);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getNursingPlanById(Long id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectNursingLevelById(id);

        return nursingLevelVo;
    }

    /**
     * 修改护理等级
     *
     * @param dto
     */
    @Override
    public void updateNursingPlan(NursingLevelDto dto) {
        //判断是否名称已存在
        Integer count = nursingLevelMapper.selectCountNurLevelNameExcludeId(dto.getId(), dto.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPLEVEL_NAME_EXICT);
        }
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    /**
     * 删除护理等级
     *
     * @param id
     */
    @Override
    public void deleteNursingPlan(Long id) {
        // 判断是否与老人绑定
        Integer count = nursingLevelMapper.selectElderCountByLevelId(id);
        if (count > 0) {
            throw new BaseException(BasicEnum.NURSINGPLEVEL_BIND_Elder);
        }
        nursingLevelMapper.deleteNursingLevelById(id);
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }


}
